﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zero.Code;
using Zero.Model;

namespace Zero.DAL
{
    /// <summary>
    /// 角色授权数据层
    /// 作者：edl
    /// 最后修改日期：2019-9-25
    /// </summary>
    public class RoleAuthorization
    {
        /// <summary>
        /// 功能模块集合
        /// </summary>
        /// <param name="RoleId">角色ID</param>
        /// <returns></returns>
        public List<modular> GetModularsList(string RoleId)
        {
            List<modular> ModularList = new List<modular>();
            using (var db = new zerodbEntities())
            {
                modular modular = null;
                if (RoleId.ToInt() == 0)
                {
                     var modulars = from m in db.modular
                                 select m;
                    foreach (var item in modulars)
                    {
                        modular = new modular();
                        modular = item;
                        ModularList.Add(modular);
                    }
                    return ModularList;
                } 
            }
                return null;
        }
        /// <summary>
        /// 通过角色ID获取岗位
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<role> GetDutyByRoleID(int RoleID)
        {
            using (var db = new zerodbEntities())
            {
                var role = from r in db.role
                           where r.R_Id == RoleID
                           select r;
                var item = role.FirstOrDefault();
                return GetDuty(item.R_OrganizedId.ToInt());
            }
        }

        /// <summary>
        /// 获取岗位
        /// </summary>
        /// <param name="OrganizedID">机构ID</param>
        /// <returns></returns>
        public List<role> GetDuty(int OrganizedID)
        {
            List<role> Dutys = new List<role>();
            using (var db = new zerodbEntities())
            {
                if (OrganizedID == 0)
                {
                    role role = null;
                    //如果为0则查询所有岗位
                    var duty = from r in db.role
                               select r;
                    foreach (var item in duty)
                    {
                        role = new role();
                        role = item;
                        Dutys.Add(role);
                    }
                }
                else
                {
                    //否则查询该角色所在机构的岗位
                    role role = null;
                    var duty = from r in db.role
                               where r.R_OrganizedId == OrganizedID
                               select r;
                    foreach (var item in duty)
                    {
                        role = new role();
                        role = item;
                        Dutys.Add(role);
                    }
                }

                return Dutys;
            }
        }
        /// <summary>
        /// 通过角色表中的唯一主键获取岗位
        /// </summary>
        /// <param name="R_ID"></param>
        /// <returns></returns>
        public role GeyDutyById(int R_ID)
        {
            using (var db = new zerodbEntities())
            {
                var duty = from r in db.role
                           where r.R_Id == R_ID
                           select r;
                return duty.FirstOrDefault();
            }
        }
    }
}
